package com.zhilianbao.leyaogo.ui.activity.me.setting;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.bql.utils.CheckUtils;
import com.bql.utils.EventManager;
import com.zhilianbao.leyaogo.R;
import com.zhilianbao.leyaogo.events.LeEventCode;
import com.zhilianbao.leyaogo.http.api.AccountApi;
import com.zhilianbao.leyaogo.http.callback.DialogCallback;
import com.zhilianbao.leyaogo.model.response.UserLoginInfo;
import com.zhilianbao.leyaogo.ui.activity.base.BaseOkHttpActivity;
import com.zhilianbao.leyaogo.utils.Utils;
import com.zhilianbao.leyaogo.utils.XToastUtils;
import com.zlb.lxlibrary.service.LeXiuInitService;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by liu on 2016/11/19.
 */
public class ChangLoginPwdOriActivity extends BaseOkHttpActivity {

    @BindView(R.id.et_input_pwd_old)
    EditText etInputPwdOld;
    @BindView(R.id.et_input_set_pwd_new)
    EditText etInputSetPwdNew;
    @BindView(R.id.tv_chang_login_pwd_commit)
    TextView tvChangLoginPwdCommit;
    @BindView(R.id.tv_chang_pwd_pho)
    TextView tvChangPwdPho;

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.activity_chang_login_pwd_ori;
    }

    @Override
    protected void initViewsAndEvents(Bundle savedInstanceState) {
        initToolbar(getString(R.string.change_login_pwd));
        etInputPwdOld.addTextChangedListener(setPwdTextWatcher);
        etInputSetPwdNew.addTextChangedListener(setPwdTextWatcher);
        tvChangLoginPwdCommit.setEnabled(false);
    }

    @OnClick({R.id.tv_chang_login_pwd_commit, R.id.tv_chang_pwd_pho})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_chang_login_pwd_commit:
                String oldPwd = etInputPwdOld.getText().toString().trim();
                String newPwd = etInputSetPwdNew.getText().toString().trim();
                if (CheckUtils.isAllPassword(newPwd)) {
                    UserLoginInfo userLoginInfo = Utils.getUserLoginInfo();
                    AccountApi.updateLoginPwd(ChangLoginPwdOriActivity.this, userLoginInfo.getUserId(), userLoginInfo.getTokenId(), oldPwd, newPwd, new DialogCallback<Object>(this) {

                        @Override
                        public void onSuccess(Object o, Call call, Response response) {
                            XToastUtils.showShortToast("修改登录密码成功,请重新登录");
                            finish();
                            outLogin();
                            Utils.gotoLogin();
                        }
                    });
                } else {
                    XToastUtils.showShortToast(getString(R.string.new_pwd_wrong));
                }

                break;
            case R.id.tv_chang_pwd_pho:
                Utils.startActivity(this, ChangLoginPwdPhoneActivity.class);
                finish();
                break;
        }
    }

    public void outLogin() {
        Utils.clear();
        //        Utils.unregisterPush();
        LeXiuInitService.getInstance().onLegoLogout();
        EventBus.getDefault().post(new EventManager(LeEventCode.MSG_CODE_OUT_LOGIN));
    }

    /**
     *
     */
    private TextWatcher setPwdTextWatcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            tvChangLoginPwdCommit.setEnabled(etInputPwdOld.getText().length() != 0 && etInputSetPwdNew.getText().length() != 0);
        }
    };

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (setPwdTextWatcher != null) {
            etInputSetPwdNew.removeTextChangedListener(setPwdTextWatcher);
            etInputPwdOld.removeTextChangedListener(setPwdTextWatcher);
            setPwdTextWatcher = null;
        }

    }

}